package com.m.system.controller;

import org.springframework.web.bind.annotation.RestController;
import com.client.job.utils.DataResult;
import com.m.system.domain.SysMenuEntity;
import com.m.system.domain.SysRoleEntity;
import com.m.system.service.SysMenuService;
import com.m.utils.UserUtil;
import com.m.utils.ValidResult;
import com.m.utils.ValidationUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * 菜单权限表Controller
 * 
 * @author monxz
 * @version 2021-01-29
 */
@RestController
@RequestMapping(value = "system/sysMenu")
@Api(tags = "菜单权限")
public class SysMenuController  {

	@Autowired
	private SysMenuService sysMenuService;

	@PostMapping(value = "/list")
	public DataResult list(SysMenuEntity sysMenuEntity) throws Exception {
		return DataResult.buildSuccess(sysMenuService.findSysMenuList(sysMenuEntity,false));
	}

	@PostMapping(value = "/save")
	public DataResult save(SysMenuEntity SysMenuEntity) throws Exception {
		// 校验接口参数
		ValidResult validResult = ValidationUtils.validateGroup(SysMenuEntity);
		if (validResult.isHasErrors()) {
			return DataResult.buildFail(validResult.getErrorMsg());
		}
		SysMenuEntity ckMenu = new SysMenuEntity();
		ckMenu.setMenuName(SysMenuEntity.getMenuName());
		if (sysMenuService.checkUnion(ckMenu) == 1) {
			return DataResult.buildFail("菜单名称重复");
		}
		return DataResult.buildByInter(sysMenuService.addSysMenu(SysMenuEntity));
	}

	@GetMapping(value = "/getUserMenu")
	public DataResult getUserMenuList(SysMenuEntity sysMenuEntity) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		sysMenuEntity = sysMenuService.findSysMenuById(sysMenuEntity);
		resMap.put("menu", sysMenuEntity);
		resMap.put("roleList", UserUtil.getUser(null).getRoleList());
		resMap.put("roleIds", UserUtil.getUser(null).getRoleList().stream().map(SysRoleEntity::getRoleId)
				.collect(Collectors.toList()));
		return DataResult.buildSuccess(resMap);
	}

	@PostMapping(value = "/update")

	public DataResult update(SysMenuEntity SysMenuEntity) throws Exception {
		// 主键判断
		if (SysMenuEntity.getMenuId() == null) {
			return DataResult.buildFail("没有对应的Id");
		}
		// 校验接口参数
		ValidResult validResult = ValidationUtils.validateGroup(SysMenuEntity);
		if (validResult.isHasErrors()) {
			return DataResult.buildFail(validResult.getErrorMsg());
		}
		SysMenuEntity ckMenu = new SysMenuEntity();
		ckMenu.setMenuName(SysMenuEntity.getMenuName());
		ckMenu.setMenuId(SysMenuEntity.getMenuId());
		if (sysMenuService.checkUnion(ckMenu) == 1) {
			return DataResult.buildFail("菜单名称重复");
		}
		return DataResult.buildByInter(sysMenuService.modifySysMenu(SysMenuEntity));
	}

	@PostMapping(value = "/delete")

	public DataResult delete(String menuId) throws Exception {
		if (menuId == null || menuId.trim().length() == 0) {
			return DataResult.buildFail("参数不能为空");
		}
		return DataResult.buildByInter(sysMenuService.delete(menuId));
	}
	
	@PostMapping(value="/treeSelect")
	@ApiOperation("查询角色菜单树")
	public DataResult  treeSelect(SysMenuEntity  menu) throws Exception {
		List<SysMenuEntity>  menuList = sysMenuService.findSysMenuList(menu,true);		
		return DataResult.buildSuccess(sysMenuService.buildMenuTreeSelect(menuList));
	}
	
	@GetMapping(value = "/roleMenu")
	public DataResult  roleMenuAndTree() throws Exception {
		Map<String, Object> res = new HashMap<>();
		res.put("checkedKeys", sysMenuService.findSysMenuList(null,false));
		res.put("menus", sysMenuService.buildMenuTreeSelect(sysMenuService.findSysMenuList(null,true)));		
		return DataResult.buildSuccess(res);
	}

}